package com.maple.exclusivesport.ui.main;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.constraint.ConstraintLayout;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.maple.exclusivesport.ContactInfoBean;
import com.maple.exclusivesport.DataBaseOpenHelper;
import com.maple.exclusivesport.R;
import com.maple.exclusivesport.SendEvent;
import com.maple.exclusivesport.adapters.ContactInfo;
import com.maple.exclusivesport.adapters.MyAdapter;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * A placeholder fragment containing a simple view.
 */
public class RecordPlaceholderFragment extends Fragment {

    private static final String ARG_SECTION_NUMBER = "section_number";
    @Bind(R.id.card_list)
    RecyclerView mRecyclerView;

    static RecordPlaceholderFragment fragment;
    public static RecordPlaceholderFragment newInstance(int index) {
        if(fragment==null){
            fragment = new RecordPlaceholderFragment();
            Bundle bundle = new Bundle();
            bundle.putInt(ARG_SECTION_NUMBER, index);
            fragment.setArguments(bundle);
        }
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        try{
            EventBus.getDefault().register(this);
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    @Override
    public View onCreateView(
            @NonNull LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View root = inflater.inflate(R.layout.fragment_record, container, false);
        ButterKnife.bind(this, root);
        mRecyclerView.setHasFixedSize(true);
        LinearLayoutManager layoutManager = new LinearLayoutManager(getContext());
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        mRecyclerView.setLayoutManager(layoutManager);
        initInfo();
        adapter = new MyAdapter(getContext(),mList);
        mRecyclerView.setAdapter(adapter);

        return root;
    }

    MyAdapter adapter;
    List<ContactInfo> mList;


    private void initInfo() {


        mList = new ArrayList<>();
        mList.addAll(DataBaseOpenHelper.getInstance(getContext()).queryContactInfoList());




    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        try{
            ButterKnife.unbind(this);
        }catch (Exception e){
            e.printStackTrace();
        }

    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void Send(SendEvent e) {
        Log.e("error", "send");
        mList.clear();
        mList.addAll(DataBaseOpenHelper.getInstance(getContext()).queryContactInfoList());
        adapter.notifyDataSetChanged();

    }
}